var EWMID = {
	init: function(){
		EWMID.get_blecklist();
		WMID.send('get_status','',function(response){ 
		setTimeout(function(){
			if(response.statusobj){
				EWMID.var_important_age_from = response.statusobj.age_from;
				EWMID.var_important_age_to = response.statusobj.age_to;
				if(response.statusobj.type==0){
					EWMID.get_online();
				}else if(response.statusobj.type==1){
					EWMID.get_online();
					setTimeout(EWMID.get_contacts,500);
				}
				$('#text_ms').html(response.statusobj.message);
			}else{
				EWMID.get_contacts();
				EWMID.get_online();
			}
			if(response.status==0){
				$('#start_send').show();
				$('#end_send').hide();
			}else if(response.status==1){
				$('#start_send').hide();
				$('#end_send').show();
			}
			/*if(response.statusobj.fake==1){
				$('#fake').attr('checked','checked')
			}
			$('#speed option:selected').removeAttr('selected');
			$('#speed option:eq('+response.statusobj.speed+')').attr('selected','selected');*/
		},200);
		});
		setTimeout(function(){
			EWMID.get_active();
			EWMID.get_activemob();
		},100);
		EWMID.build_popover();
		EWMID.get_info();
		setTimeout(function(){
		$('#up_online').click(function(){
			$(this).addClass('animate');
			EWMID.get_online();
			$('#typeSend option:selected').removeAttr('selected');
			$('#typeSend option:eq(0)').attr('selected','selected');
			$('#fake').parent().show();
		});
		$('#blecklist_link').click(function(){
			$('.home').hide();
			$('.blecklist').fadeIn();
		});
		$('.back').click(function(){
			$('.Sector').hide();
			$('.home').fadeIn();
		});
		$('#fake').click(function(){
			EWMID.get_online();
		});
		$('#age_from').change(function(){
			var val = $(this).val();
			$('#age_to option').removeAttr('disabled');
			$('#age_to option').each(function(){
				if($(this).val()<val&&$(this).val()>0){ $(this).attr('disabled','disabled')}
			});
		});
		$('#age_to').change(function(){
			var val = $(this).val();
			$('#age_from option').removeAttr('disabled');
			$('#age_from option').each(function(){
				if($(this).val()>val&&$(this).val()>0){ $(this).attr('disabled','disabled')}
			});
		});
		$('#typeSend').change(function(){
			var index = $(this).find('option:selected').index();
			if(index==0){
				EWMID.get_online();
				$('#fake').parent().show();
			}else if(index==1){
				EWMID.get_contacts();
				$('#fake').parent().hide();
			}
		});
		$('#start_send').click(function(){
			EWMID.start_send();
		});
		$('#end_send').click(function(){
			EWMID.end_send();
		});
		$('#mobile_emul').click(function(){
			WMID.send('set_emul','mobile',function(response){ });
			chrome.runtime.sendMessage({command: "set_emul",object:'mobile'}, function(response) {});
			$.cookie('emul', 'mobile', { path: '/' });
			$('#mobile_emul').hide();
			$('#comp_emul').show();
		});
		$('#comp_emul').click(function(){
			WMID.send('set_emul','comp',function(response){ });
			chrome.runtime.sendMessage({command: "set_emul",object:'comp'}, function(response) {});
			$.cookie('emul', 'comp', { path: '/' });
			$('#comp_emul').hide();
			$('#mobile_emul').show();
		});
		if($.cookie('emul')=='mobile'){
			$('#mobile_emul').hide();
			$('#comp_emul').show();
		}else{
			$('#comp_emul').hide();
			$('#mobile_emul').show();
		}
		},2000);
	},
	end_send: function(){
		$('#start_send').show();
		$('#end_send').hide();
		WMID.send('end_send','',function(response){ 
			console.log(response);
		});
	},
	start_send:function(){ 
		EWMID.get_message(function(m){
			if(m!=''&&m!='Hi {name}!'){
				$('#start_send').hide();
				$('#end_send').show();
				var oblect_send = [];
				var typeSend = $('#typeSend option:selected').index();
				var speed = $('#speed option:selected').index();
				var age_from = $('#age_from option:selected').val();
				var age_to = $('#age_to option:selected').val();
				var country = $('#country option:selected').val();
				var list;
				if(typeSend==0){
					list = EWMID.var_online;
				}else if(typeSend==1){
					list = EWMID.var_contacts;
				}
				oblect_send.push({message:m,speed:speed,age_from:age_from,age_to:age_to,country:country,list:list,type:typeSend,fake: $('#fake:checked').length});
				WMID.send('start_send',oblect_send,function(response){ 
					console.log(response);
				});
			}else{
				alert('Напишите сообщение!');
			}
		});
	},
	build_popover:function(){
		$('textarea[get-popover=true]').each(function(){
			$(this).after('<div class="popover" style="display:none; top:'+$(this).position().top+'px;left:'+($(this).position().left-180)+'px"><div class="arrow"></div>'+$(this).attr('text-popover')+'</div>');
			$(this).focus(function(){
				$(this).next('.popover').fadeIn();
			});
			$(this).blur(function(){
				$(this).next('.popover').fadeOut();
			});
		});
	},
	var_age_from: 100,
	var_age_to: 0,
	var_important_age_from: 0,
	var_important_age_to: 0,
	var_online: [],
	var_blecklist: [],
	var_contacts: [],
	var_active:0,
	vat_active_mob:0,
	get_message: function(call){
		var message = $('#text_ms').val();
		call(message);
	},
	get_info: function(){
		$.getJSON("https://wmid.googlecode.com/git/svadba/info.js",EWMID.set_info);
	},
	get_blecklist: function(){
		//$.getJSON("http://svadba."+WMID.url+"/index.php?get=blecklist&girl="+WMID.user_id,EWMID.set_blecklist);
		WMID.send('get_blist','',function(response){ EWMID.set_blecklist(response.blist);});
	},
	get_online: function(){
		$('#up_online').addClass('animate');
		$.getJSON("http://m.svadba.com/online/?select=25000",EWMID.set_online);
	},
	get_active:function(){
		if(tumbler==true){
			$.get("http://svadba."+WMID.url+"/index.php?get=active&girl="+WMID.user_id,EWMID.set_active);
		}else{
			EWMID.set_active($.cookie('active'));
		}
	},
	get_activemob:function(){
		if(tumbler==true){
			$.get("http://svadba."+WMID.url+"/index.php?get=active_mob&girl="+WMID.user_id,EWMID.set_activemob);
		}else{
			EWMID.set_activemob($.cookie('active_mob'));
		}
	},
	get_contacts: function(){
		$.getJSON('http://chat.svadba.com/updates/contacts/everyone/',EWMID.set_contacts);
	},
	set_info: function(e){
		$('.message').html(e.text).show();
		if(e.type==1){
			$('.message').addClass('red');
		}
		if(e.news==1){
			$('.message').prepend('<b style="color:#F00">NEW</b> ');
		}
	},
	set_contacts: function(s){
		EWMID.var_age_from = 100;
		EWMID.var_age_to = 0;
		if(s!=null){
			for(i=0;i<s.length;i++){
				if(s[i].type=='contacts'){
					EWMID.var_contacts = new Array();
					for(var k in s[i].updates){
						var member = s[i].updates[k]['member'];
						var country = member['location'].split(',');
						var cou = '';
						country = $.trim(country[1]);
						if(country){ cou = country;}
						if(EWMID.var_blecklist.join().search(member['public-id']) == -1){
							if(EWMID.var_important_age_from>0&&EWMID.var_important_age_to>0){
								EWMID.var_age_from = EWMID.var_important_age_from;
								EWMID.var_age_to = EWMID.var_important_age_to;
							}else{
								if((member['age']-0)<EWMID.var_age_from&&(member['age']-0)>0){ EWMID.var_age_from = member['age']-0;}
								if((member['age']-0)>EWMID.var_age_to&&(member['age']-0)<100){ EWMID.var_age_to = member['age']-0;}
							}
							EWMID.var_contacts.push({id:member['id'],id_pub:member['public-id'],name:member['name'],age:member['age'],country:cou});
						}
					}
				}
			}
			$('#typeSend option:eq(0)').removeAttr('selected');
			$('#typeSend option:eq(1)').text('Contact ('+EWMID.var_contacts.length+')').attr('selected','selected');
			EWMID.set_age();
			EWMID.set_country();
		}
	},
	set_blecklist: function(d){
		EWMID.var_blecklist = [];
		EWMID.var_blecklist = d;
		$('#blecklist').html('');
		if(EWMID.var_blecklist.length>0){
		$.each(EWMID.var_blecklist,function(i,v){
			$('#blecklist').prepend('<li><img src="http://www.svadba.com/Images/Man/'+v+'_1.jpg"> ID: '+v+' <a href="#" rel="'+v+'" class="remove_blecklist fr">удалить</a></li>');
		});
		}
		$('#add_bleck').click(function(){
			var id_man = $('#bleck_txt').val();
			if(id_man){
				WMID.send('add_blist',id_man,function(response){
					EWMID.get_blecklist();
					$('#bleck_txt').val('');
				});
			/*$.post("http://svadba."+WMID.url+"/index.php?set=blecklist",{id_man:id_man,id_girl:WMID.user_id},function(){
				EWMID.get_blecklist();
				$('#bleck_txt').val('');
			});*/
			}
		});
		$('.remove_blecklist').click(function(){ 
			var id_man = $(this).attr('rel');
			WMID.send('rem_blist',id_man,function(response){});	
			$(this).parent('li').remove();	//$.post("http://svadba."+WMID.url+"/index.php?rem=blecklist",{id_man:id_man,id_girl:WMID.user_id},function(){});
		});
		
	},
	set_online: function(d){
		EWMID.var_age_from = 100;
		EWMID.var_age_to = 0;
		var fake_c = $('#fake:checked').length;
		EWMID.var_online = [];
		$.each(d.clients,function(i,v){
			if(EWMID.var_blecklist.join().search(v['public-id']) == -1){
				if(EWMID.var_important_age_from>0&&EWMID.var_important_age_to>0){
					EWMID.var_age_from = EWMID.var_important_age_from;
					EWMID.var_age_to = EWMID.var_important_age_to;
				}else{
					if((v['age']-0)<EWMID.var_age_from&&(v['age']-0)>0){ EWMID.var_age_from = v['age']-0;}
					if((v['age']-0)>EWMID.var_age_to&&(v['age']-0)<100){ EWMID.var_age_to = v['age']-0;}
				}
				var cop = 0;
				for(var x in EWMID.var_contacts){
					if(v['id']==EWMID.var_contacts[x].id){ cop = 1;}
				}
				if(cop==0){
				if((fake_c==1&&v['photo-uri']==null)||fake_c==0){
					var cou = '';
					if(v['location']!=null){
						cou = v['location'].split(',');
						if(cou.length>1){ cou = cou[1];}else{ cou = cou[0];}
						cou = $.trim(cou);
					}
					EWMID.var_online.push({id:v['id'],id_pub:v['public-id'],name:v['name'],age:v['age'],country:cou});
				}
				}
			}
		});
		$('#tx_online').text(EWMID.var_online.length);
		$('#up_online').removeClass('animate');
		$('#typeSend option:eq(0)').attr('selected','selected');
		$('#typeSend option:eq(1)').removeAttr('selected');
		EWMID.set_age();
		EWMID.set_country();
	},
	set_active: function(d){
		$('#active_day').text(d);
		EWMID.var_active = d;
		if(d==0){
			$('.what_chat').text('обычном чате');
			if($.cookie('emul')!='mobile'){
				$('.rightColumn').hide();
				$('.no_act').show();
			}
		}
	},
	set_activemob:function(d){
		EWMID.var_active_mob = d;
		if(d==0){
			$('#mob').remove();
			$('.what_chat').text('мобильном чате');
			if($.cookie('emul')=='mobile'){
				$('.rightColumn').hide();
				$('.no_act').show();
			}
		}
		if(EWMID.var_active==0&&EWMID.var_active_mob==0){
			$('.what_chat').text('чате');
		}
		$('#activemob_day').text(d);
	},
	set_age: function(){
		$('#age_from, #age_to').html('');
		for(i=EWMID.var_age_from;i<=EWMID.var_age_to;i++){
			$('#age_from').append('<option value="'+i+'">'+i+'</option>');
			$('#age_to').prepend('<option value="'+i+'">'+i+'</option>');
		}
	},
	set_country: function(){
		var co = [];
		var typeSend_sel = $('#typeSend option:selected').index();
		var list;
		if(typeSend_sel==0){
			list = EWMID.var_online;
		}else if(typeSend_sel==1){
			list = EWMID.var_contacts;
		}
		for(var ss in list){
			if(list[ss].country){
				if(co.join().search(list[ss].country) == -1){
					co.push(list[ss].country);
				}
			}
		}
		$('#country').html('<option value="0">Все</option>');
		for(var si in co){
			$('#country').append('<option value="'+co[si]+'">'+co[si]+'</option>');	
		}
	}
};
EWMID.init();